<!-- 自镜自架 -->
<template>
  <el-dialog title="商品查询" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :inline="true" :model="dataForm">
      <el-row style="margin-top: 15px;" v-if="dataForm.ctgId == 3">
        <el-form-item>
          <el-radio-group v-model="eyesType" @change="getDataList">
            <el-radio :label="1">右眼镜片</el-radio>
            <el-radio :label="2">左眼镜片</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" ref="goodsTable" border v-loading="dataListLoading" highlight-current-row
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="45" :selectable="checkSelectable"></el-table-column>
        <el-table-column prop="code" label="商品编码"></el-table-column>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="goodsPrice" label="销售价格">
          <template> 0 </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
  </el-dialog>
</template>

<script>
  import {
    filterParams
  } from '@/util/tools'

  import { goodsFetchList } from '@/api/eims/goods'
  export default {
    data() {
      return {
        visible: false,
        paramsData: {},
        eyesType: 1,
        dataForm: {
          ctgId: '',
          opticMaterial: '',
          opticType: '',
          key: '',
          person: '',
          address: ''
        },
        supplyId: '',
        goodsId: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        selectList1: [],
        selectList2: []
      }
    },
    methods: {
      init(data) {
        this.paramsData = data.paramsData
        this.tableData = data.tableData
        this.eyesType = 1
        this.visible = true
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.dataForm.ctgId = this.paramsData.ctgId
        this.dataForm.selfFlag = 1
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = filterParams(this.dataForm)

        goodsFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
            this.$nextTick(() => {
              this.tableData.forEach(item => {
                for (let i = 0; i < this.dataList.length; i++) {
                  if (this.dataList[i].id == item.id) {
                    this.$refs.goodsTable.toggleRowSelection(this.dataList[i], true)
                  }
                }
              })
            })
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //
      checkSelectable(row) {
        if (this.goodsId && this.dataForm.ctgId == '3') {
          return row.id == this.goodsId
        } else {
          return true
        }
      },
      handleSelectionChange(val) {
        // 镜片/隐形类
        if (this.dataForm.ctgId == '3') {
          if (val.length > 0) {
            this.supplyId = val[0].supplyId
            this.goodsId = val[0].id

            val[0].goodsQuantity = '1'
            val[0].discountRate = '1'
            val[0].discountPrice = '0'
            val[0].removePrice = '0'
            val[0].goodsPrice = '0'
            val[0].goodsId = val[0].id
            if (this.eyesType == '1') {
              val[0].eyesType = '1'
              val[0].glassFlag = 'R'
              val[0].goodsDescription = '右眼镜片'
            } else {
              val[0].eyesType = '2'
              val[0].glassFlag = 'L'
              val[0].goodsDescription = '左眼镜片'
            }
          } else {
            this.supplyId = ''
            this.goodsId = ''
          }
          let select = 'selectList' + this.eyesType
          this[select] = val
          let arr = this.selectList1.concat(this.selectList2)
          this.$emit('refreshForm', {
            'value': arr,
            'ctgId': this.dataForm.ctgId
          })
        } else {
          // 其他类
          val.forEach(item => {
            item.goodsQuantity = '1'
            item.discountRate = '1'
            item.discountPrice = '0'
            item.removePrice = '0'
            item.goodsId = item.id
            item.goodsPrice = '0'
            val[0].goodsDescription = '镜架'
            val[0].glassFlag = 'F'
          })

          this.$emit('refreshForm', {
            'value': val,
            'ctgId': this.dataForm.ctgId
          })
        }


      }
    }
  }
</script>
